<template>
  <div class="login-container">
    <div class="left-panel">
      <div class="logo-container">
        <div class="logo-wrapper">
          <svg viewBox="0 0 40 40" class="logo-icon">
            <path d="M20 5L25 15H15L20 5Z" fill="#409EFF"/>
            <path d="M15 15H5V25H15L10 35H30L25 25H35V15H25L30 5H10L15 15Z" fill="#409EFF"/>
          </svg>
          <h1 class="logo">IPFoxy</h1>
        </div>
      </div>
      <div class="service-info">
        <h2>企业级全球代理<span class="highlight">IP</span>服务</h2>
        <ul class="feature-list">
          <li>覆盖全球200+国家和地区</li>
          <li>千万独享代理IP地址</li>
          <li>99.9%网络稳定运行</li>
          <li>7X24小时全时段技术支持</li>
        </ul>
      </div>
    </div>
     <div class="login-form-container">
    <el-card class="login-card">
      <div class="card-header">
        <h2>登录</h2>
        <el-link type="primary" href="#" class="register-link">立即注册</el-link>
      </div>

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入正确的邮箱地址/手机号"
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            show-password
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <!-- 登录按钮 -->
        <el-form-item>
          <el-button
            type="primary"
            @click="handleLogin"
            :loading="loading"
            block
            class="login-button"
          >
            登录
          </el-button>
        </el-form-item>

        <!-- 记住我 + 忘记密码 -->
        <el-form-item class="form-footer">
          <el-checkbox v-model="loginForm.rememberMe" class="remember-me">
            保留我的登录状态
          </el-checkbox>
          <el-link type="primary" href="#" class="forgot-password">
            忘记密码？
          </el-link>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
  </div>
</template>
<script setup>
import { ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import { User, Lock } from '@element-plus/icons-vue'; // 导入图标
import { login } from '../api/login';  // 导入API接口

// 表单引用
const loginFormRef = ref(null);

// 表单数据
const loginForm = reactive({
  username: '',
  password: '',
  rememberMe: false
});

// 加载状态
const loading = ref(false);


// 表单规则
const loginRules = {
  username: [
    { required: true, message: '请输入正确的邮箱地址/手机号', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        // 简单验证：可以是邮箱或手机号
        const emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
        const phonePattern = /^1[3-9]\d{9}$/;
        if (emailPattern.test(value) || phonePattern.test(value)) {
          callback();
        } else {
          callback(new Error('请输入有效的邮箱地址或手机号'));
        }
      },
      trigger: 'blur'
    }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少为6位', trigger: 'blur' }
  ]
};

// 处理登录
const handleLogin = async () => {
  // 验证表单
  try {
    await loginFormRef.value.validate();
    loading.value = true;

    // 调用登录API
    const result = await login(loginForm);

    // 登录成功处理
    ElMessage.success('登录成功');
    console.log('登录成功结果:', result);

    // 保存登录状态
    if (loginForm.rememberMe) {
      localStorage.setItem('userInfo', JSON.stringify(result.userInfo));
      localStorage.setItem('token', result.token);
    } else {
      sessionStorage.setItem('userInfo', JSON.stringify(result.userInfo));
      sessionStorage.setItem('token', result.token);
    }

    // 跳转到首页或其他页面
    // router.push('/home');
  } catch (error) {
    // 处理验证失败或登录失败
    if (error.name === 'ValidateError') {
      // 表单验证失败
      ElMessage({ message: error.message, type: 'error', duration: 3000 });
    } else {
      // 登录失败
      const errorMsg = error.message || '未知错误';
      ElMessage({ message: `登录失败: ${errorMsg}`, type: 'error', duration: 3000 });
    }
  } finally {
    loading.value = false;
  }
};

</script>

<style scoped>
.login-container {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  height: 100vh;
  width: 100vw;
  background-color: #f8f9fa;
  overflow: hidden;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  max-width: 1200px;
  margin: 0 auto;
}

.left-panel {
  -webkit-box-flex: 0;
      -ms-flex: 0 0 40%;
          flex: 0 0 40%;
  max-width: 600px;
  background: -webkit-gradient(linear, left top, right bottom, from(#409EFF), to(#90C8FF));
  background: -webkit-linear-gradient(top left, #409EFF, #90C8FF);
  background: linear-gradient(135deg, #409EFF, #90C8FF);
  color: white;
  padding: 40px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
      -ms-flex-direction: column;
          flex-direction: column;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  position: relative;
  overflow: hidden;
  min-width: 300px;
  border-radius: 12px 0 0 12px;
  height: 65%;
}

.service-info h2 {
  font-size: 32px;
  margin-bottom: 32px;
  font-weight: bold;
  text-align: center;
  line-height: 1.2;
}

.feature-list li {
  margin-bottom: 12px;
  font-size: 16px;
  opacity: 0.9;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
}

.login-card {
  width: 100%;
  max-width: 420px;
  border-radius: 12px;
  overflow: hidden;
  background-color: white;
  padding: 40px 30px;
  margin: 0 auto;
  border-radius: 0 12px 12px 0 ;
  height: 595px;
}

.login-button {
  background: -webkit-gradient(linear, left top, right bottom, from(#409EFF), to(#67C2FF));
  background: -webkit-linear-gradient(top left, #409EFF, #67C2FF);
  background: linear-gradient(135deg, #409EFF, #67C2FF);
  border: none;
  height: 48px;
  border-radius: 8px;
  font-size: 16px;
  margin-bottom: 15px;
  width: 100%;
  -webkit-transition: opacity 0.3s;
  transition: opacity 0.3s;
  -webkit-appearance: none;
     -moz-appearance: none;
          appearance: none;
  color: white;
  font-weight: 500;
}

.login-button:hover {
  opacity: 0.9;
  cursor: pointer;
}

.google-login-button {
  height: 48px;
  border-radius: 8px;
  font-size: 16px;
  margin-top: 16px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  width: 100%;
  -webkit-transition: all 0.3s;
  transition: all 0.3s;
  -webkit-appearance: none;
     -moz-appearance: none;
          appearance: none;
  background-color: white;
  border: 1px solid #409EFF;
  color: #409EFF;
}

.google-login-button:hover {
  background-color: #f0f7ff;
  cursor: pointer;
}
</style>
